package cn.xuexiyuan.springstudy.core.demo05.service;

import cn.xuexiyuan.springstudy.core.demo05.entity.AbstractPrefix;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.util.concurrent.atomic.AtomicInteger;

/**
 *  标注 @Lazy 注解,作用是告诉容器只有在要使用时才加载
 *
 * @Scope 注解设置POJO的作用域
 *      - singleton 单例,默认值
 *      - prototype 每次请求都创建一个新的bean
 *      - request 每次请求都创建一个新的bean
 *      - session 每次会话都创建一个新的bean
 *      - globalSession
 */
@Lazy
@Component
@Scope("prototype")
public class SequenceGenerator {
    private final AtomicInteger counter = new AtomicInteger();

    @Autowired
    private AbstractPrefix prefix;



    public String getSequence(){
        StringBuilder builder = new StringBuilder();
        builder.append(prefix.getPrefix()).append(counter.getAndIncrement());

        return builder.toString();
    }


}
